package com.ruoyi.storage.bicycle.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.basicData.ruleSetting.domain.AccessoryDataVo;
import com.ruoyi.basicData.ruleSetting.service.IAccessorySupplierRelevanceService;
import com.ruoyi.basicData.supplier.domain.Supplier;
import com.ruoyi.basicData.supplier.mapper.SupplierMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.outbound.outbound.mapper.OutboundMapper;
import com.ruoyi.storage.bicycle.domain.vo.BicycleFittingVo;
import com.ruoyi.storage.bicycle.domain.vo.BicycleListVo;
import com.ruoyi.storage.bicycle.domain.vo.BicycleVo;
import com.ruoyi.storage.fitting.domain.Fitting;
import com.ruoyi.storage.fitting.domain.FittingValue;
import com.ruoyi.storage.fitting.mapper.FittingMapper;
import com.ruoyi.storage.tickets.domain.Tickets;
import com.ruoyi.storage.tickets.mapper.TicketsMapper;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.storage.bicycle.mapper.BicycleMapper;
import com.ruoyi.storage.bicycle.domain.Bicycle;
import com.ruoyi.storage.bicycle.service.IBicycleService;

/**
 * ControlAcquisitionService业务层处理
 * 
 * @author zzx
 * @date 2024-01-29
 */
@Service
@RequiredArgsConstructor
public class BicycleServiceImpl implements IBicycleService {
    private final BicycleMapper bicycleMapper;
    private final TicketsMapper ticketsMapper;
    private final IAccessorySupplierRelevanceService accessorySupplierRelevanceService;
    private final ISysDictTypeService sysDictTypeService;
    private final ISysDictDataService sysDictDataService;
    private final SupplierMapper supplierMapper;
    private final OutboundMapper outboundMapper;
    private final FittingMapper fittingMapper;

    /**
     * 查询ControlAcquisition
     *
     * @param bicycleId ControlAcquisition主键
     * @return ControlAcquisition
     */
    @Override
    public Bicycle selectBicycleByBicycleId(String bicycleId) {
        Bicycle bicycle = bicycleMapper.selectBicycleByBicycleId(bicycleId);
        if (bicycle == null) {
            bicycle.setBicycleId(bicycleId);
        }
        List<FittingValue> fittingValues = fittingMapper.selectFittingValueByBicycle(bicycleId);
        Map<String, String> map = new HashMap<>();
        for (FittingValue fittingValue : fittingValues) {
            map.put(fittingValue.getFittingFabienName(), fittingValue.getFitting());
        }
        bicycle.setMap(map);
        return bicycle;
    }

    /**
     * 查询ControlAcquisition列表
     *
     * @param bicycle ControlAcquisition
     * @return ControlAcquisition
     */
    @Override
    public List<Bicycle> selectBicycleList(Bicycle bicycle) {
        List<Bicycle> bicycles = bicycleMapper.selectBicycleList(bicycle);
        for (Bicycle bicycleListVo : bicycles) {
            List<FittingValue> fittingValues = fittingMapper.selectFittingValueByBicycle(bicycleListVo.getBicycleId());
            Map<String, String> map = new HashMap<>();
            for (FittingValue fittingValue : fittingValues) {
                map.put(fittingValue.getFittingFabienName(), fittingValue.getFitting());
            }
            bicycleListVo.setMap(map);
        }
        return bicycles;
    }

    /**
     * 新增ControlAcquisition
     *
     * @param bicycle ControlAcquisition
     * @return 结果
     */
    @Override
    public int insertBicycle(Bicycle bicycle) {
        bicycle.setAudit(0L);
        return bicycleMapper.updateBicycle(bicycle);
    }

    /**
     * 修改ControlAcquisition
     *
     * @param bicycle ControlAcquisition
     * @return 结果
     */
    @Override
    public int updateBicycle(Bicycle bicycle) {
        return bicycleMapper.updateBicycle(bicycle);
    }

    @Override
    public AjaxResult updateBicycle(BicycleVo bicycleVo) {
        Bicycle bicycle = new Bicycle();
        BeanUtils.copyProperties(bicycleVo, bicycle);
        //配件信息
        List<AccessoryDataVo> accessoryDataVos = bicycleVo.getAccessoryDataVos();
        if (accessoryDataVos == null || accessoryDataVos.isEmpty()) {
            return AjaxResult.error("提交失败，未进行合法性校验~");
        }

        List<String> noSupplierLabels = accessoryDataVos.stream()
                .filter(accessoryDataVo -> !accessorySupplierRelevanceService.hasSupplier(accessoryDataVo.getAccessoryType(), accessoryDataVo.getData()))
                .map(accessoryDataVo -> sysDictDataService.selectDictLabel("accessory_type", accessoryDataVo.getAccessoryType()))
                .collect(Collectors.toList());

        if (!noSupplierLabels.isEmpty()) {
            String errorMsg = String.join("、", noSupplierLabels) + "不存在供应商";
            return AjaxResult.error(errorMsg);
        }
        bicycle.setStatus(2L);
        updateBicycle(bicycle);

        for (AccessoryDataVo accessoryDataVo : accessoryDataVos) {
            if (!accessoryDataVo.getAccessoryType().equals("controllerId")) {
                FittingValue fittingValue = new FittingValue();
                fittingValue.setFittingId(accessoryDataVo.getFittingId());
                fittingValue.setBicycleId(bicycle.getBicycleId());
                fittingValue.setFitting(accessoryDataVo.getData());
                if (fittingMapper.selectFittingExist(fittingValue)) {
                    //修改配件信息
                    fittingMapper.updateFittingValue(fittingValue);
                } else {
                    //添加配件信息
                    fittingMapper.insertFittingValue(fittingValue);
                }
            }
        }
        return AjaxResult.success();

    }

    /**
     * 批量删除ControlAcquisition
     *
     * @param bicycleIds 需要删除的ControlAcquisition主键
     * @return 结果
     */
    @Override
    public int deleteBicycleByBicycleIds(String[] bicycleIds) {
        return bicycleMapper.deleteBicycleByBicycleIds(bicycleIds);
    }

    /**
     * 删除ControlAcquisition信息
     *
     * @param bicycleId ControlAcquisition主键
     * @return 结果
     */
    @Override
    public int deleteBicycleByBicycleId(String bicycleId) {
        return bicycleMapper.deleteBicycleByBicycleId(bicycleId);
    }

    /**
     * 查看订单结案情况
     *
     * @param ticketsId
     * @return
     */
    @Override
    public AjaxResult selectTicketsclose(String ticketsId) {
        Tickets tickets = ticketsMapper.selectTicketsByTicketsId(ticketsId);
        if (tickets.getClose() == 2) {
            return AjaxResult.error("车辆ID相关订单已结案");
        } else {
            return AjaxResult.success();
        }

    }

    /**
     * 审核
     *
     * @param bicycleIds
     * @return
     */
    @Override
    public AjaxResult updateBicycleAudit(String[] bicycleIds) {
        bicycleMapper.updateBicycleAudit(bicycleIds);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateBicycleUnAudit(String[] bicycleIds) {
        bicycleMapper.updateBicycleUnAudit(bicycleIds);
        return null;
    }

    /**
     * 查询四码采集
     *
     * @param bicycle
     * @return
     */
    @Override
    public List<Bicycle> selectBicycleListorder(Bicycle bicycle) {
        List<Bicycle> bicycleFittingVos = bicycleMapper.selectBicycleListorder(bicycle);
        for (Bicycle bicycleListVo : bicycleFittingVos) {
            List<FittingValue> fittingValues = fittingMapper.selectFittingValueByBicycle(bicycleListVo.getBicycleId());
            Map<String, String> map = new HashMap<>();
            for (FittingValue fittingValue : fittingValues) {
                map.put(fittingValue.getFittingFabienName(), fittingValue.getFitting());
            }
            bicycleListVo.setMap(map);
        }
        return bicycleFittingVos;
    }

    @Override
    public AjaxResult updateStatusById(String[] bicycleIds) {
        bicycleMapper.updateStatusById(bicycleIds);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult selectBicycleExist(String keyWord) {
        if (StringUtils.isEmpty(keyWord)) {
            return AjaxResult.error("未检测到查询参数");
        }
        Bicycle bicycle = bicycleMapper.selectBicycleExist(keyWord);
        int count = fittingMapper.selectFittingExistBybicycleId(bicycle.getBicycleId());
        if (bicycle == null && count == 0) {
            return AjaxResult.error("该订单不存在");
        } else if (5 != bicycle.getStatus()) {
            return AjaxResult.error("订单还未出库");
        } else {
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult selectBicycleById(String keyWord) {
        Map<String, BicycleListVo> map = new HashMap<>();
        Map<String, List<Supplier>> hmasp = new HashMap<>();
        String bicycleId = fittingMapper.selectFittingById(keyWord);
        BicycleListVo bicycle = null;
        if (bicycleId == null || bicycleId.isEmpty()) {
            bicycle = bicycleMapper.selectBicycleById(keyWord);
        } else {
            bicycle = bicycleMapper.selectBicycleByBicycleIdAsVo(bicycleId);
        }

        Date outboundDate = outboundMapper.selectoutboundDateByBicycle(bicycle.getBicycleId());
        bicycle.setOutboundDate(outboundDate);
        //动态配件数据查询
        List<FittingValue> fittingValues = fittingMapper.selectFittingValueByBicycle(bicycle.getBicycleId());
        bicycle.setFittingValues(fittingValues);
        //电池供应商
        List<Supplier> BatterySuppliers = supplierMapper.selectSuppliersById(bicycle.getBatteryId());
        //车架号供应商
        List<Supplier> FrameSuppliers = supplierMapper.selectSuppliersById(bicycle.getFrameId());
        //控制器供应商
        List<Supplier> ControllerSuppliers = supplierMapper.selectSuppliersById(bicycle.getControllerId());
        //电机供应商
        List<Supplier> MotorSuppliers = supplierMapper.selectSuppliersById(bicycle.getMotorId());
        //仪表供应商
        List<Supplier> MeterSuppliers = supplierMapper.selectSuppliersById(bicycle.getMeterId());
        hmasp.put("BatterySuppliers", BatterySuppliers);
        hmasp.put("FrameSuppliers", FrameSuppliers);
        hmasp.put("ControllerSuppliers", ControllerSuppliers);
        hmasp.put("MotorSuppliers", MotorSuppliers);
        hmasp.put("MeterSuppliers", MeterSuppliers);
        bicycle.setSupplierMap(hmasp);
        map.put("orderDetail", bicycle);
        return AjaxResult.success(map);
    }

    /**
     * 四码采集审核
     *
     * @param bicycleIds
     * @return
     */
    @Override
    public AjaxResult updateBicycleOrderAudit(String[] bicycleIds) {
        bicycleMapper.updateBicycleOrderAudit(bicycleIds);
        return AjaxResult.success();
    }

    /**
     * 四码采反集审核
     *
     * @param bicycleIds
     * @return
     */
    @Override
    public AjaxResult updateBicycleOrderUnAudit(String[] bicycleIds) {
        bicycleMapper.updateBicycleOrderUnAudit(bicycleIds);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult selectFitting(AccessoryDataVo accessoryDataVo) {
        if (accessorySupplierRelevanceService.hasSupplier(accessoryDataVo.getAccessoryType(), accessoryDataVo.getData())) {
            return AjaxResult.success();
        } else {
            return new AjaxResult();
        }
    }

    @Override
    public AjaxResult selectFittings() {
        Fitting fitting = new Fitting();
        fitting.setDelFlag(0L);
        List<Fitting> fittings = fittingMapper.selectFittings(fitting);
        return AjaxResult.success(fittings);
    }

}
